package com.yfp.auth.controller;

import cn.hutool.core.lang.Assert;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.yfp.auth.form.CaptchaBody;
import com.yfp.auth.form.LoginBody;
import com.yfp.auth.service.SysLoginService;
import com.yfp.common.core.constant.SecurityConstants;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.JwtUtils;
import com.yfp.common.core.utils.StringUtils;
import com.yfp.common.security.auth.AuthUtil;
import com.yfp.common.security.service.TokenService;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.operation.api.RemoteGroupService;
import com.yfp.operation.domain.EnterpriseVO;
import com.yfp.operation.domain.GroupRegisterBody;
import com.yfp.system.api.model.LoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * yfp-group 团餐登录注册模块
 *
 *
 */
@Tag(name = "yfp-group 团餐登录注册模块")
@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;
    @Resource
    private RemoteGroupService remoteGroupService;


    @Operation(summary = "h5-获取短信验证码", description = "企业账号获取短信验证码接口")
    @PostMapping("/captcha")
    public R<String> captcha(@RequestBody CaptchaBody captchaBody) {
        return R.ok(sysLoginService.getGroupCaptcha(captchaBody));
    }

    @Operation(summary = "h5-注册账号", description = "企业账号注册接口")
    @PostMapping("/register")
    public R<WxPayUnifiedOrderV3Result.JsapiResult> register(@RequestBody GroupRegisterBody groupRegisterBody) {
        // 注册账号
        R<WxPayUnifiedOrderV3Result.JsapiResult> jsapiResult = sysLoginService.groupRegister(groupRegisterBody);
        return jsapiResult;
    }

    @Operation(summary = "h5-登录", description = "企业账号登录接口")
    @PostMapping("/login/captcha")
    public R<Map<String, Object>> loginCaptcha(@RequestBody LoginBody loginBody) {
        // 用户登录
        LoginUser userInfo = sysLoginService.loginGroupCaptcha(loginBody);
        // 获取登录token
        Map<String, Object> token = tokenService.createToken(userInfo);
        R<EnterpriseVO> enterpriseUserByUserId = remoteGroupService.getEnterpriseUserByUserId(userInfo.getUserid(), SecurityConstants.INNER);
        Assert.isFalse(R.isError(enterpriseUserByUserId), enterpriseUserByUserId.getMsg());
        token.put("enterpriseUser", enterpriseUserByUserId.getData());
        return R.ok(token);
    }

    @Operation(summary = "h5-退出", description = "企业账号退出接口")
    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }
}
